﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SystemContainer.Models;

namespace SystemContainer.Controllers
{
    public class UploadFileController : Controller
    {
        private SystemEntities db = new SystemEntities();

        //
        // GET: /UploadFile/

        public ActionResult Index()
        {
            IEnumerable<Algorithm> a = db.Algorithms;
            IEnumerable<CheckInData> c = db.CheckIns;
            IEnumerable<PhotoData> p = db.PhotoDatas;
            IEnumerable<City> ci = db.Cities;


            ViewBag.Algorithms = new SelectList(a, "AlgorithmID", "name");
            ViewBag.CheckIns = new SelectList(c, "CheckInDataID", "name");
            ViewBag.PhotoDatas = new SelectList(p, "PhotoDataID", "name");
            ViewBag.city = new SelectList(ci, "CityID", "locationName");

            var list = db.list;
            return View(list);
        }


        [HttpPost]
        public ActionResult UploadAlgorithm(HttpPostedFileBase up)
        {
            var file = Request.Files[0];
            if (file != null && file.ContentLength > 0)
            {
                string path = Constants.serverPath + Constants.algorithmPath;
                string filename = Request.Files[0].FileName;
                file.SaveAs(path + filename);
                Algorithm a = new Algorithm { Name = file.FileName, dir = path };
                if (SaveAlgorithm(a))
                {
                    ViewData["uploadAlgorithm"] = "success";
                    return RedirectToAction("Index");
                }
                  
            }

            ViewData["uploadAlgorithm"] = "failed";
            return RedirectToAction("Index");
        }

        //保存到数据库
        private bool SaveAlgorithm(Algorithm a)
        {
            bool success = false;

            if (ModelState.IsValid)
            {
                db.Algorithms.Add(a);
                db.SaveChanges();
                success = true;
            }

            return success;
        }

        private bool SaveCheckInData(CheckInData d)
        {
            bool success = false;

            if (ModelState.IsValid)
            {
                db.CheckIns.Add(d);
                db.SaveChanges();
                success = true;
            }

            return success;
        }

        private bool SavePhotoData(Photo d)
        {
            bool success = false;

            if (ModelState.IsValid)
            {
                db.Photos.Add(d);
                db.SaveChanges();
                success = true;
            }

            return success;
        }

        [HttpPost]
        public ActionResult UploadCheckInData(HttpPostedFileBase up)
        {
            var file = Request.Files[0];
            if (file != null && file.ContentLength > 0)
            {
                string path = Constants.serverPath + Constants.datasetPath;
                string filename = Request.Files[0].FileName;
                file.SaveAs(path + filename);
                CheckInData d = new CheckInData { Name = file.FileName, dir = path };

                if (SaveCheckInData(d))
                    ViewData["uploadAlgorithm"] = "success";
                   return RedirectToAction("Index");
            }

            ViewData["uploadDataset"] = "failed";
            return RedirectToAction("Index");
        }


        [HttpPost]
        public ActionResult UploadPhotoData(HttpPostedFileBase up)
        {
            var file = Request.Files[0];
            if (file != null && file.ContentLength > 0)
            {
                string path = Constants.serverPath + Constants.datasetPath;
                string filename = Request.Files[0].FileName;
                file.SaveAs(path + filename);
                Photo d = new Photo { Name = file.FileName, dir = path };

                if (SavePhotoData(d))
                    ViewData["uploadAlgorithm"] = "success";
                return RedirectToAction("Index");
            }

            ViewData["uploadDataset"] = "failed";
            return RedirectToAction("Index");
        }

       
        public ActionResult Match(FormCollection formcollection)
        {
            if (formcollection["Algorithms"] == null)
                return Content("error! Choose an Algorithm File.");
            if (formcollection["CheckIns"] == null)
                return Content("error! Choose an CheckInData File.");
            if (formcollection["PhotoDatas"] == null)
                return Content("error! Choose an PhotoData File.");
            if (formcollection["City"] == null)
                return Content("error! Choose an City.");

            int algorithmId = int.Parse(formcollection["Algorithms"].ToString());
            Algorithm a = db.Algorithms.Find(algorithmId);
            int checkInDataId = int.Parse(formcollection["CheckIns"].ToString());
            CheckInData c = db.CheckIns.Find(checkInDataId);
            int photoId = int.Parse(formcollection["PhotoDatas"].ToString());
            PhotoData p = db.PhotoDatas.Find(photoId);
            int cityId =int.Parse(formcollection["City"].ToString());
            City city = db.Cities.Find(cityId);
            //string resultPath = formcollection["ResultPath"].ToString();
            //TODO whether resultPath should be used

            if (ModelState.IsValid)
            {
                Combination b = new Combination { AlgorithmName = a.Name, AlgorithmDir = a.dir, CheckInDataName = c.Name, CheckInDataPath = c.dir, PhotoDataName = p.Name, City = city.locationName, ResultPath = "" };

                db.list.Add(b);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return Content("error!");
        }


        /*public ActionResult Details(int id)
        {
            var al = db.Algorithms.Find(id);
            IEnumerable<DataSet> datas = al.datas;
            IEnumerable<DataSet> allDatas = db.DataSets.ToList();
            
            IEnumerable<DataSet> unmatchedDatas = new List<DataSet>();

            unmatchedDatas = allDatas.Except(datas).ToList();// 除了以上data的其他data ????
            ViewBag.UnmatchedDatas = new SelectList(unmatchedDatas, "datasetId", "name");
            ViewBag.algorithmId = id;

            return View(datas);
            
        }
        */

        public ActionResult Delete(int id)
        {
            var al = db.Algorithms.Find(id);
            db.Algorithms.Remove(al);
            db.SaveChanges();

            //ViewData["Message"] = "成功删除算法" + al.Name;
            return RedirectToAction("Index");
        }

        public ActionResult DeleteDatasetRelation(int combinationId)
        {
            var item = db.list.Find(combinationId);
            db.list.Remove(item);
            db.SaveChanges();
            return RedirectToAction("Index");
             
        }
    }
}
